package com.xwh.b2c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.errorprone.annotations.Var;
import com.xwh.b2c.dto.AdminLoginDTO;
import com.xwh.b2c.entity.Admin;
import com.xwh.b2c.entity.AdminRole;
import com.xwh.b2c.exceptions.BusinessException;
import com.xwh.b2c.handlers.BaseContextHandler;
import com.xwh.b2c.mapper.AdminMapper;
import com.xwh.b2c.service.AdminRoleService;
import com.xwh.b2c.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xwh.b2c.utils.JwtUtil;
import com.xwh.b2c.vo.AdminInfoVO;
import com.xwh.b2c.vo.AdminLoginVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;

/**
 * <p>
 * 管理员用户表 服务实现类
 * </p>
 *
 * @author wuyunbin
 * @since 2025-07-07
 */
@Slf4j
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Resource
    private JwtUtil jwtUtil;

    @Resource
    private BCryptPasswordEncoder passwordEncoder;

    @Resource
    private AdminRoleService adminRoleService;
    //TODO 实现Admin login登录逻辑 返回头像和token
    @Override
    public AdminLoginVO login(AdminLoginDTO adminLoginDTO) {
        Admin admin = null;
        //前端发一个邮箱和密码 返回一个token和头像
        AdminLoginVO adminLoginVO = new AdminLoginVO();
        //管理员不存在
        if(adminLoginDTO.getUsername() != null){
            LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();//这里的泛型要写Admin
            wrapper.eq(Admin::getEmail,adminLoginDTO.getUsername());
            admin = this.getOne(wrapper);
            if(admin == null){
                log.info("账号不存在或密码错误");
                throw new BusinessException("账号不存在或密码错误");
            }

            //密码不对
            log.info("密码：{}", passwordEncoder.encode(adminLoginDTO.getPassword()));

            //matches(明文，密文)
            if(!passwordEncoder.matches(adminLoginDTO.getPassword(),admin.getPassword())){
                log.info("密码错误");
                throw new BusinessException("账号不存在或密码错误");
            }
            //管理员存在且密码也没错
            //签发token
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",admin.getId());
            //jwt token 不能放敏感的数据 id可以放
            String token = jwtUtil.generateToken(map);
            adminLoginVO.setAvatar(admin.getAvatar());
            adminLoginVO.setToken(token);
        }
        return adminLoginVO;
    }

    @Override
    public AdminInfoVO info() {
        Long id = BaseContextHandler.get();
        Admin admin = this.getById(id);

        AdminInfoVO adminInfoVO = new AdminInfoVO();
        adminInfoVO.setName(admin.getAdminName());
        adminInfoVO.setAvatar(admin.getAvatar());
        adminInfoVO.setEmail(admin.getEmail());

        //admin的role_id转成 tb_admin_role中对应的 角色名称 1-admin ...
        AdminRole adminRole = adminRoleService.getById(admin.getRoleId());
        adminInfoVO.setRole(adminRole.getName());
        return adminInfoVO;
    }


}
